package com.hrma.controller;

import com.github.pagehelper.Page;
import com.hrma.entity.*;
import com.hrma.services.TravelApplicationService;
import com.hrma.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/travelApplication")
@Slf4j
public class TravelApplicationController {
    private final TravelApplicationService travelApplicationService;

    public TravelApplicationController(TravelApplicationService travelApplicationService) {
        this.travelApplicationService = travelApplicationService;
    }

    @PostMapping("/add")
    public Result<TravelApplication> add(@Validated @RequestBody TravelApplication data){
        log.debug("收到add请求，请求参数：{}", data);
        return Result.ok(travelApplicationService.add(data));
    }

    @PatchMapping("/update")
    public Result<?> update(@Validated @RequestBody TravelApplication data){
        log.debug("收到update请求，请求参数：{}", data);
        travelApplicationService.update(data);
        return Result.ok(null);
    }

    @GetMapping("/select")
    public Result<List<SelectResult>> select() {
        log.debug("收到select请求！");
        return Result.ok(travelApplicationService.select());

    }

    @PostMapping("/list")
    public Result<ListWithCount<TravelApplicationVO>> getList(@RequestBody TravelApplicationDTO data) {
        log.debug("收到list请求，请求参数：{}", data);
        Page<TravelApplicationVO> res = travelApplicationService.getList(data);

        return Result.ok(new ListWithCount<>(res.getTotal(), res));
    }

    @PostMapping("/PersonalList")
    public Result<ListWithCount<TravelApplicationVO>> getPersonalList(@RequestBody TravelApplicationDTO data) {
        log.debug("收到list请求，请求参数：{}", data);
        Page<TravelApplicationVO> res = travelApplicationService.getPersonalList(data);

        return Result.ok(new ListWithCount<>(res.getTotal(), res));
    }
}
